import com.google.appengine.api.datastore.KeyFactory
import com.google.appengine.api.images.* 

if (params["id"]) {

 try {
	 
   def id = Long.parseLong(params["id"])	 
   def key = KeyFactory.createKey("Perfil", id)
   def perfil = datastore.get(key)
   
   if (perfil.imaxe) {
	   
	   //obten a fotoPerfil
	   def foto = obtenImaxe(perfil.imaxe)
	   
	   byte[] bytes = foto.imaxe.getBytes()
	   
	   def width = params['w']
       def height = params['h']
                           
       def ancho = 90
       def alto = 90
       
       if ( width && height ) {
		 ancho = Integer.parseInt(width) 
		 alto = Integer.parseInt(height) 
       }
	   
	   
       if ( width && height ){ 
    	     Image oldImage = ImagesServiceFactory.makeImage(bytes) 
    	   	 CompositeTransform cp = ImagesServiceFactory.makeCompositeTransform()
    	   	 Transform resize = ImagesServiceFactory.makeResize(ancho,alto)
    	   	 cp.concatenate( resize )
    	   	 Image pic = images.applyTransform( cp, oldImage, ImagesService.OutputEncoding.valueOf("JPEG"))
			 //response.setContentType("application/octet-stream")
    	   	 response.setContentType("image/jpeg")
			 sout << pic.imageData
       } 	   
	   
	   
   } else {
	   html.img(src:'/images/default_profile.png')
   }

   } catch (Throwable t) {
	   println "error..........." + t
    //forward to some error page...
   }
} else {
	html.div {
	}
}

def obtenImaxe(def idImaxe) {
	   def key = KeyFactory.createKey("FotoPerfil", idImaxe)
	   def foto = datastore.get(key)
	
}

	
